package server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Vector;

import controller.ControllerKunde;
import model.Kunde;

public class Client {

	private final static String IP = "20.49.185.100";

	public static void main(String[] args){
	
		Socket socket = null;
		
	    try {
			Kunde kunde = new Kunde();
	    	System.out.println("Client: Kunde erzeugt. " + kunde.getNachname());
	    	
	    	socket = new Socket( IP, 5550 );
	    	
	      	ObjectOutputStream clientOutputStream = new ObjectOutputStream(socket.getOutputStream());
	      	System.out.println("Neuer Client Outputstream erstellt");
	      	
	      	ObjectInputStream clientInputStream = new ObjectInputStream(socket.getInputStream());
	      	System.out.println("Neuer Client Inputstream erstellt");
	      	
	      	ControllerKunde ks = new ControllerKunde();
	      	clientOutputStream.writeObject(ks);
	      	
	      	try {
	      		
	      		Object inputObj =  clientInputStream.readObject();
	      		System.out.println(inputObj.toString());
				Vector<Kunde> vector = (Vector<Kunde>) inputObj;
				kunde = (Kunde) vector.get(0);
				System.out.println(kunde.getNachname());
				
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
	      	
	      	System.out.println("Client: Kunde empfangen. " + kunde.getNachname());

	      	clientOutputStream.close();
	      	clientInputStream.close();
	    }
	    catch (UnknownHostException e) {
            System.err.println("Host nicht bekannt.");
	    }
	    catch (IOException e) {
	    	e.printStackTrace();
            System.err.println("IO-Probleme.");
	    }
	    finally
	    {
	      if (socket != null)
	        try { 
	        	socket.close(); 
	        	System.out.println("Socket wurde geschlossen.");
	        } catch ( IOException e ) {
	        	System.err.println("Socket konnte nicht geschlossen werden.");
	        	e.printStackTrace(); 
	        }
	    }
	}
}
